package com.honghe.task;

import com.honghe.entity.DocPat;
import com.honghe.entity.DoctorPatient;
import com.honghe.mapper.DocPatMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class Task {
    @Autowired
    private DocPatMapper docPatMapper;
    @Scheduled(cron = "0 0 0 L 1 ? ")//每年1月的最后一天执行一次
    public void disposeVisitDate(){
        log.warn("删除所有订单数据");
        docPatMapper.deleteAll();
    }
    @Scheduled(cron = "0 0 0 * * ? ")//没日执行一次
    public void disposeVisitStatus(){
        log.info("处理未完成订单");
        Map<String,Object> map=new HashMap<>();
        map.put("status",DoctorPatient.NO_DONE);
        map.put("begin", LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        map.put("end", LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
        List<DocPat> docPat = docPatMapper.getByMap(map);
        List<Long> ids = docPat.stream().map(dp -> dp.getId()).collect(Collectors.toList());
        docPatMapper.updateStatusBatch(DoctorPatient.PAT_DID_NOT_ARRIVE,ids);
    }
    @Scheduled(cron = "0 0 * * * ? ")//每一小时自动删除temp包下的文件
    public void disposeTempFile(){
        File file=new File("D:\\javaweb_temp\\hoh-registered\\hos-server\\src\\main\\resources\\temp");
        File[] files = file.listFiles();//获取文件夹中的一级文件对象
        if (files != null) {
            for (File f : files) {
                if (f.isFile()){
                    f.delete();
                }
            }
            log.info("已完成临时文件的删除");
        }
    }
}
